package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Options;
import com.itheima.domain.db.SoulQuestion;
import com.itheima.mapper.SoulQuestionMapper;
import com.itheima.service.db.OptionsService;
import com.itheima.service.db.SoulQuestionService;
import com.itheima.vo.OptionsVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SoulQuestionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class SoulQuestionServiceImpl implements SoulQuestionService {

    @Autowired
    private SoulQuestionMapper soulQuestionMapper;

    @Autowired
    private OptionsService optionsService;

    // 查询问题集合
    @Override
    public List<SoulQuestionVo> findByQuestionnaireAndUserId(Integer questionnaireId) {
        // 创建查询条件
        QueryWrapper<SoulQuestion> qw = new QueryWrapper<>();
        qw.eq("question_type",questionnaireId);
        // 查询
        List<SoulQuestion> soulQuestionList = soulQuestionMapper.selectList(qw);
        // 声明vo
        List<SoulQuestionVo> voList = new ArrayList<>();
        // 遍历
        if (CollectionUtil.isNotEmpty(soulQuestionList)) {
            for (SoulQuestion soulQuestion : soulQuestionList) {
                // 创建vo
                SoulQuestionVo vo = new SoulQuestionVo();
                Integer soulQuestionId = soulQuestion.getId();
                vo.setId(soulQuestionId.toString());
                vo.setQuestion(soulQuestion.getContent());
                List<OptionsVo> optionsVoList = optionsService.findByQuestionId(soulQuestionId);
                vo.setOptions(optionsVoList);
                // 存入voList
                voList.add(vo);
            }
        }
        return voList;
    }

    // 根据问题id查问题
    @Override
    public SoulQuestion findQuestionId(Integer questionId) {
        return soulQuestionMapper.selectById(questionId);
    }

    //查询题库对应问题
    @Override
    public PageBeanVo findQuestion(Integer pageNum, Integer pageSize, String bankId) {
        //分页
        Page<SoulQuestion> page = new Page<>(pageNum, pageSize);
        //条件
        QueryWrapper<SoulQuestion> qw = new QueryWrapper<>();
        qw.eq("question_type", Integer.parseInt(bankId));
        page = soulQuestionMapper.selectPage(page,qw );
        PageBeanVo pageBeanVo = new PageBeanVo(pageNum, pageSize, page.getTotal(), page.getRecords());
        return pageBeanVo;
    }

    //根据id删题
    @Override
    public void deleteQuestionById(Integer id) {
        soulQuestionMapper.deleteById(id);
    }

    //批量删题
    @Override
    public void deleteMoreQuestion(Integer[] ids) {
        if (ids != null){
            for (int i = 0; i < ids.length; i++) {
                soulQuestionMapper.deleteById(ids[i]);
            }
        }
    }

    //添加题目
    @Override
    public void addQuestion(SoulQuestion soulQuestion) {
        System.out.println(soulQuestion);
        soulQuestionMapper.insert(soulQuestion);
    }

}
